/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mxgraph.examples.swing;

/**
 *
 * @author somesh
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ExecuteShellComand {

    public static void main(String[] args) {

        ExecuteShellComand obj = new ExecuteShellComand();

        String domainName = "10.250.250.52";

        //in mac oxs
        String command = "ping -c 3 " + domainName;

        //in windows
        //String command = "ping -n 3 " + domainName;

        String output = obj.executeCommand(command);

        //System.out.println(output);

    }

    private String executeCommand(String command) {
        ArrayList<String> pingOutPut = new ArrayList<String>();
        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
                pingOutPut.add(line);
            }
            if (pingOutPut.get(pingOutPut.size() - 2).contains("100% packet loss") ) {
                System.out.println("unreachable");
            } else {
                System.out.println("reachable");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }
}